#include <stdio.h>
#include <ucontext.h>
#include "list.h"

void add_list(struct thread_elt *e, struct thread_elt **list) {

  if(*list == NULL) {
    *list = e;
  }
  else {
    struct thread_elt *tmp = *list;
    while(tmp->next != NULL) {
      tmp = tmp->next;
    }
    tmp->next = e;
  }

}


int search_list(struct thread_elt* thread,struct thread_elt * list)
{
  struct thread_elt * tmp =list;
  while (tmp != NULL) {
    if( thread == tmp)
      return 1;
    if(search_list(thread,tmp->waiting_begin))
      return 1;
    tmp=tmp->next;
  }
  return 0;
}




void remove_list(struct thread_elt *e, struct thread_elt **list) {

  struct thread_elt *tmp = *list;
  if(*list == e) {
    *list = (*list)->next;
  }
  else {
    if(tmp != NULL){
      while(tmp->next != e && tmp->next != NULL)
	tmp = tmp->next;
      if (tmp->next != NULL)
	tmp->next = tmp->next->next;
    }
  }

  e->next = NULL;
}
